package com.yhy.produce.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import com.yhy.common.core.result.Result;
import com.yhy.common.rabbitmq.config.DefaultDelayConfig;
import com.yhy.common.rabbitmq.utils.RabbitMqUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "发送消息")
@Slf4j
@RestController
@RequestMapping("/send")
@RequiredArgsConstructor
public class SendController {

    /**
     * 消息体为对象。配置MessageConverter为Jackson2JsonMessageConverter即可
     */
    @GetMapping("/delay/biz1")
    public Result<?> sendDelay1Message() {
        JSONObject data = new JSONObject();
        data.set("name", "jax");
        data.set("time", LocalDateTimeUtil.now().toString());
        RabbitMqUtil.sendDelayMsg("default.biz.exchange", "default.biz.routing_key.1",
                data, RandomUtil.randomNumbers(10), 1000 * 10L);
        return Result.ok();
    }

    @GetMapping("/delay/biz2")
    public Result<?> sendDelay2Message() {
        JSONObject data = new JSONObject();
        data.set("name", "jack");
        data.set("time", LocalDateTimeUtil.now().toString());
        RabbitMqUtil.sendDelayMsg("default.biz.exchange", "default.biz.routing_key.2",
                data, RandomUtil.randomNumbers(10), 1000 * 60 * 2L);
        return Result.ok();
    }

    @GetMapping("/plugin/delay")
    public Result<?> sendDelay3Message() {
        JSONObject data = new JSONObject();
        data.set("name", "jack");
        data.set("time", LocalDateTimeUtil.now().toString());

        RabbitMqUtil.sendDelayMsgByPlugin(DefaultDelayConfig.DEFAULT_EXCHANGE,
                DefaultDelayConfig.DEFAULT_ROUTING_KEY, data, RandomUtil.randomNumbers(10), 1000 * 60 * 2L);
        return Result.ok();
    }

    @GetMapping("/plugin/delay/1")
    public void sendDelay4Message() {
        JSONObject data = new JSONObject();
        data.set("name", "jack");
        data.set("time", LocalDateTimeUtil.now().toString());

        RabbitMqUtil.sendDelayMsgByPlugin(DefaultDelayConfig.DEFAULT_EXCHANGE,
                DefaultDelayConfig.DEFAULT_ROUTING_KEY, data, RandomUtil.randomNumbers(10), 1000 * 60 * 2L);
    }

}